import re
from TinyShop17.CASE import LoginTinyShop


class BuyGoods:
    def __init__(self, url='http://192.168.43.214/ty18/TinyShop_v1.7'):
        self.session = LoginTinyShop.Login().start()[0]
        self.url = url

    def chooseGoods(self):
        url = '%s/index.php?con=index&act=get_ask' % self.url
        data = {
            'id': '16',
            'page': 'a.txt'
        }
        self.session.post(url, data=data).json()

    def addGoods(self):  # 选了商品进入订单支付页面
        url = '%s/index.php?con=index&act=cart_add' % self.url
        data = {
            'num': 'a.txt',
            'id': '55'
        }
        self.session.post(url, data=data)

    def getToken(self):
        url = '%s/index.php?con=simple&act=order' % self.url
        headers = {
            'Referer': '%s/index.php?con=simple&act=cart' % self.url,
            'Host': '192.168.43.214',
            'Connection': 'Keep-Alive',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Upgrade-Insecure-Requests': 'a.txt',
            'Accept-Encoding': 'gzip, deflate'
        }
        html = self.session.get(url, headers=headers).text
        passent = re.compile(r"<input type='hidden' name='tiny_token_order' value='(.*)'/>")
        self.tiny_token_order = passent.findall(html)[0]

    def payGoods(self):
        url = '%s/index.php?con=simple&act=get_voucher' % self.url
        data = {
            'amount': '55',
            'page': 'a.txt'
        }
        html = self.session.post(url, data=data).json()
        # 第二个跳转
        url1 = '%s/index.php?con=ajax&act=calculate_fare' % self.url
        data1 = {
            'weight': '200',
            'id': '2'
        }
        html1 = self.session.post(url1, data=data1).json()
        url = '%s/index.php?con=simple&act=order_act' % self.url
        data = {
            'address_id': '4',
            'payment_id': 'a.txt',
            'invoice_type': '0',
            'tiny_token_order': self.tiny_token_order
        }
        html = self.session.post(url, data=data)
        passent = re.compile('id=(.*)')
        self.order_id = passent.findall(html.url)[0]

    def payMoney(self):
        url = '%s/index.php?con=payment&act=dopay' % self.url
        data = {
            'order_id': self.order_id,
            'payment_id': 'a.txt',
            'channelBank': 'ICBC'
        }
        html = self.session.post(url, data=data).text
        passent = re.compile("value='(.*)'")
        self.content = passent.findall(html)

    def payBlance(self):
        url = '%s/index.php?con=payment&act=pay_balance' % self.url
        data = {
            'attach': self.content[0],
            'total_fee': self.content[1],
            'order_no': self.content[2],
            'return_url': self.content[3],
            'sign': self.content[4]
        }
        print(data)
        html = self.session.post(url, data=data).text
        passent = re.compile(r'<input type="hidden" name="attach" value="(.*)" />')
        content = passent.findall(html)[0]
        content = content.split('"')
        # print(content[0], content[6], content[12], content[18], content[24], content[30], content[36])
        url1 = '%s/index.php?con=payment&act=callback&payment_id=a.txt' % self.url
        data1 = {
            'attach': content[0],
            'total_fee': content[6],
            'order_no': content[12],
            'return_url': content[18],
            'order_status': content[24],
            'sign': content[30],
            'tiny_token_redirect': content[36]
        }
        self.html1 = self.session.post(url1, data=data1).text
        passent1 = re.compile(r'<i class="icon-order-0"></i>(.*) &nbsp;')
        buynumber = passent1.findall(self.html1)[0]
        print("订单编号： %s" % buynumber)

    def compile(self):
        if '支付成功，订购已完成' in self.html1:
            print('支付成功')
        else:
            print('支付失败')

    def start(self):
        # try:
        self.chooseGoods()
        self.addGoods()
        self.getToken()
        self.payGoods()
        self.payMoney()
        self.payBlance()
        self.compile()
    # except Exception:
    #     print('购物失败')


if __name__ == "__main__":
    BuyGoods().start()
